package com.mfh.auto.create.mvvm.autocreatemvvm

import com.intellij.openapi.actionSystem.AnAction
import com.intellij.openapi.actionSystem.AnActionEvent
import com.intellij.openapi.actionSystem.LangDataKeys
import com.intellij.openapi.actionSystem.PlatformDataKeys
import com.intellij.openapi.roots.ModuleRootManager
import com.intellij.openapi.ui.DialogWrapper
import com.intellij.psi.PsiDirectory
import com.intellij.psi.PsiFile
import com.intellij.psi.PsiManager
import com.mfh.auto.create.mvvm.autocreatemvvm.enums.EHMType
import com.mfh.auto.create.mvvm.autocreatemvvm.enums.EPlatform
import com.mfh.auto.create.mvvm.autocreatemvvm.enums.EType
import com.mfh.auto.create.mvvm.autocreatemvvm.ui.*
import com.mfh.auto.create.mvvm.autocreatemvvm.utils.CreateFileUtils
import com.mfh.auto.create.mvvm.autocreatemvvm.utils.CreateFileUtilsV2
import java.awt.event.WindowAdapter
import java.awt.event.WindowEvent
import javax.swing.JComponent
import javax.swing.JTextField

class AutoMVVMV2 : AnAction() {

    override fun actionPerformed(event: AnActionEvent) {
        Config.name = ""
        Config.desc = ""
        val project = event.getData(PlatformDataKeys.PROJECT) ?: return

        val dataContext = event.dataContext
        val module = LangDataKeys.MODULE.getData(dataContext) ?: return

        val directory = when (val navigatable = LangDataKeys.NAVIGATABLE.getData(dataContext)) {
            is PsiDirectory -> navigatable
            is PsiFile -> navigatable.containingDirectory
            else -> {
                val root = ModuleRootManager.getInstance(module)
                root.sourceRoots
                    .asSequence()
                    .mapNotNull {
                        PsiManager.getInstance(project).findDirectory(it)
                    }.firstOrNull()
            }
        } ?: return

        SampleDialog(event) {
            CreateFileUtilsV2.createFile(directory)
        }.show()
    }

    class SampleDialog(
        e: AnActionEvent,
        private val callBack: () -> Unit
    ) : DialogWrapper(e.project) {

        private var nameInput: JTextField? = null
        private var descInput: JTextField? = null

        init {
            title = "Create MVVM V2"
            init()

            window.addWindowListener(object : WindowAdapter() {
                override fun windowOpened(e: WindowEvent?) {
                    nameInput?.requestFocus()
                }
            })
        }

        override fun createCenterPanel(): JComponent {
            return jBorderLayout {
                topContainer {
                    jVerticalLinearLayout {
                        jHorizontalLinearLayout {
                            jLabel("名称： ")
                            jTextInput().apply {
                                nameInput = this
                            }
                        }
                        jHorizontalLinearLayout {
                            jLabel("备注： ")
                            jTextInput().apply {
                                descInput = this
                            }
                        }
                    }
                }
                bottomContainer {
                    jHorizontalLinearLayout {
                        jLabel("类型： ")

                        jButtonGroup {
                            jRadioButton("Activity", Config.type == EType.ACTIVITY, {
                                Config.type = EType.ACTIVITY
                            })
                            jRadioButton("Fragment", Config.type == EType.FRAGMENT, {
                                Config.type = EType.FRAGMENT
                            })
                            jRadioButton("ViewHolder", Config.type == EType.HOLDER, {
                                Config.type = EType.HOLDER
                            })
                            jRadioButton("Dialog", Config.type == EType.DIALOG, {
                                Config.type = EType.DIALOG
                            })
                        }
                    }
                }
            }
        }

        override fun doOKAction() {
            super.doOKAction()
            Config.name = nameInput?.text ?: ""
            Config.desc = descInput?.text ?: ""
            callBack.invoke()
        }
    }
}
